﻿using _2013_ece_csharp_snake;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    class rect : System.Windows.Forms.PictureBox
    {
        Panel panel;

        public rect(Panel panel, int size, Color c)
        {
            ((System.ComponentModel.ISupportInitialize)(this)).BeginInit();
            this.BackColor = c;
            this.Location = new System.Drawing.Point(100, 100);
            this.Size = new System.Drawing.Size(size, size);
            this.TabIndex = 2;
            this.TabStop = false;
            this.SetVisibleCore(false);
            this.panel = panel;
            panel.Controls.Add(this);
        }

        public rect(Panel panel, int size, Color c, int x, int y)
        {
            ((System.ComponentModel.ISupportInitialize)(this)).BeginInit();
            this.BackColor = c;
            this.Location = new System.Drawing.Point(x, y);
            this.Size = new System.Drawing.Size(size, size);
            this.TabIndex = 2;
            this.TabStop = false;
            this.panel = panel;

            panel.Controls.Add(this);
        }

        public void setVisible(bool b)
        {
            this.SetVisibleCore(b);
        }

        public void randomPosition(IList<rect> walls)
        {
            Random rd = new Random();
            int X = -1;
            int Y = -1;

            setVisible(false);

            bool col = false;

            while (col == false)
            {
                while (X < 0 )
                {
                    X = rd.Next(panel.Right - panel.Left - this.Width) + panel.Left;
                }


                while (Y < 0)
                {
                    Y = rd.Next(panel.Bottom - panel.Top - this.Width) + panel.Top;
                }

                col = true;
                for (int i = 0; i < walls.Count; i++)
                {
                    if (GameZone.checkCollision(walls[i].Top, Y, // - this.Height,
                                                walls[i].Bottom, Y + this.Height,
                                                walls[i].Right, X + this.Height,
                                                walls[i].Left, X)) // - this.Height))
                    {
                        col = false;
                        break;
                    }

                }
            }

            this.Left = X;
            this.Top = Y;

            setVisible(true);

        }
    
        // Check if the new cherry is on the snake before putting it
    public void randomPosition(IList<rect> walls, IList<rect> body)
        {
            bool success = false;
            
            Random rd = new Random();
            
            int X = -1;
            int Y = -1;

            setVisible(false);

            bool col = false;
            
                while (col == false)
                {

                    {
                        while (X < 0)
                        {
                            X = rd.Next(panel.Right - panel.Left - this.Width) + panel.Left;
                        }


                        while (Y < 0)
                        {
                            Y = rd.Next(panel.Bottom - panel.Top - this.Width) + panel.Top;
                        }

                        if (body.Count <= 1) success = true;
                        for (int i = 0; i < body.Count; i++)
                            if (body[i].Location == new System.Drawing.Point(X, Y))
                                success = true;
                    }
                



                col = true;
                for (int i = 0; i < walls.Count; i++)
                {
                    if (GameZone.checkCollision(walls[i].Top, Y, // - this.Height,
                                                walls[i].Bottom, Y + this.Height,
                                                walls[i].Right, X + this.Height,
                                                walls[i].Left, X)) // - this.Height))
                    {
                        col = false;
                        break;
                    }

                }
            }

            this.Left = X;
            this.Top = Y;

            setVisible(true);

        }
    }
}
